#    Copyright 2008, 2009 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPGIS.
#    COOPGIS is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPGIS is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPGIS.  If not, see <http://www.gnu.org/licenses/>.

#************************BEGIN Add Include Files to Path************************
import sys
NeededPaths=['/opt/coopgis/djangoproject/include', '/opt/coopgis/djangoproject']
for path in NeededPaths:
    if(not path in sys.path):
        sys.path.append(path)#************************END   Add Include Files to Path************************
import COOPGIS_Ajax, COOPGIS_FormMisc, COOPGIS_FormGeography, COOPGIS_FormAssembly, COOPGIS_GPS, COOPGIS_Misc
import COOPGIS_DynamicTemplate, COOPGIS_ReportLab, COOPGIS_String, COOPGIS_Update, COOPGIS_FormMaintenance
import os, csv, shutil, datetime, time, reportlab, djangoproject.settings, random
from django.contrib.gis.db import models
from django.contrib.gis.geos import *
from django.http import HttpResponse
from django.contrib import admin
from django.template import Context, loader
import djangoproject.settings
import _mysql as mysql
from models import *
from django import forms

def ConditionEditor(request):
    ApplicationName=djangoproject.settings.COOPGISConditionEditorApplicationName
    UserNameOrIPAddress, DefaultMap=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=True, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName) 
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName, DefaultMap)
    if(not applicationPermission):  #Show view of Map and Form if no Permissions, do not perform any form processing
        MapFileName, LayerName=COOPGIS_Misc.GenerateDynamicMapFile(ApplicationName, UserNameOrIPAddress)
        OpenLayersZoom=COOPGIS_Ajax.GetOpenLayersZoom(None, None)   
        COOPGIS_DynamicTemplate.UpdateConditionEditorTemplate(1, '')
        t = loader.get_template('/opt/coopgis/djangoproject/templates/conditioneditor.html')
        c = Context({'OpenLayersZoom': OpenLayersZoom, 'form': form,
                     'MapFileName': MapFileName, 'LayerName': LayerName, 'LoginStatus':LoginStatus})
        return HttpResponse(t.render(c))
    SelectionStation, SelectionSpan, GPSStatus = COOPGIS_FormGeography.SetSelectionStationAndSpan(form)
    COOPGIS_FormGeography.GetAndSetDefaultMap(form, ApplicationName, UserNameOrIPAddress)
    SelectionStation, SelectionSpan, GPSStatus=COOPGIS_FormGeography.SetSelectionStationAndSpan(form)
    OpenLayersZoom=COOPGIS_Ajax.GetOpenLayersZoom(SelectionStation, SelectionSpan)      
    if(SelectionStation):
        point = SelectionStation.Point
    else:
        point=None

    NumberOfConditionsListedOnForm=COOPGIS_FormMaintenance.GetNumberOfConditionsListedOnForm(point)
    for conditionFormNumber in range(0, NumberOfConditionsListedOnForm + 1):
        COOPGIS_FormMaintenance.ProcessConditionEditorFormEntry(form, conditionFormNumber, SelectionStation, UserNameOrIPAddress,ApplicationName)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName, DefaultMap, point=point)
    COOPGIS_FormMisc.PopulateMiscFormFields(form)
    COOPGIS_Misc.UpdateSelection(SelectionStation, SelectionSpan, UserNameOrIPAddress)
    #Now Rebuild the final form after processing:
    NumberOfConditionsListedOnForm=COOPGIS_FormMaintenance.GetNumberOfConditionsListedOnForm(point)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName, DefaultMap, point=point)
    COOPGIS_FormMaintenance.PopulateConditions(form, SelectionStation, NumberOfConditionsListedOnForm)
    COOPGIS_DynamicTemplate.UpdateConditionEditorTemplate(NumberOfConditionsListedOnForm, UserNameOrIPAddress)
    MapFileName, LayerName=COOPGIS_Misc.GenerateDynamicMapFile(ApplicationName, UserNameOrIPAddress)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/conditioneditor' + UserNameOrIPAddress + '.html')
    c = Context({'OpenLayersZoom': OpenLayersZoom, 'form': form,
                 'LoginStatus':LoginStatus, 'MapFileName': MapFileName, 'LayerName': LayerName,
                 'GPSStatus':GPSStatus, 
                 'ServerIndicator': djangoproject.settings.COOPGISServerIndicator,})
    return HttpResponse(t.render(c))

def ConditionDispatch(request):
    ApplicationName=djangoproject.settings.COOPGISConditionDispatchApplicationName
    UserNameOrIPAddress=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=False, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName) 
    form, NumberOfConditions=COOPGIS_FormMisc.BuildForm(request, ApplicationName)
    if(not applicationPermission):  #Show view of Form if no Permissions, do not perform any form processing
        COOPGIS_DynamicTemplate.UpdateConditionDispatchTemplate(NumberOfConditions, '')
        t = loader.get_template('/opt/coopgis/djangoproject/templates/conditiondispatch.html')
        c = Context({'form': form, 'LoginStatus':LoginStatus})
        return HttpResponse(t.render(c))
    for conditionFormNumber in range(0, NumberOfConditions):
        COOPGIS_FormMaintenance.ProcessConditionDispatchFormEntry(form, conditionFormNumber, UserNameOrIPAddress)
    COOPGIS_FormMaintenance.PopulateDispatchConditions(form, request, NumberOfConditions)
    COOPGIS_DynamicTemplate.UpdateConditionDispatchTemplate(NumberOfConditions, '')   
    t = loader.get_template('/opt/coopgis/djangoproject/templates/conditiondispatch.html')
    c = Context({'form': form, 'LoginStatus':LoginStatus})
    return HttpResponse(t.render(c))

def ConditionCreate(request, Conditionid=None):
    ApplicationName=djangoproject.settings.COOPGISConditionCreateApplicationName
    UserNameOrIPAddress, DefaultMap=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=True, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName) 
    form=COOPGIS_FormMisc.BuildForm(request, djangoproject.settings.COOPGISConditionEditorApplicationName, DefaultMap)
    if(not applicationPermission):  #Show view of Map and Form if no Permissions, do not perform any form processing
    #    MapFileName, LayerName=COOPGIS_Misc.GenerateDynamicMapFile(ApplicationName, UserNameOrIPAddress)
    #    OpenLayersZoom=COOPGIS_Ajax.GetOpenLayersZoom(None, None)   
    #    COOPGIS_DynamicTemplate.UpdateConditionCreateTemplate(1, '')
        t = loader.get_template('/opt/coopgis/djangoproject/templates/conditioncreate.html')
        #c = Context({'OpenLayersZoom': OpenLayersZoom, 'form': form,
        #             'MapFileName': MapFileName, 'LayerName': LayerName, 'LoginStatus':LoginStatus})
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))
    SelectionStation, SelectionSpan, GPSStatus = COOPGIS_FormGeography.SetSelectionStationAndSpan(form)
    if(not SelectionStation):
        SelectionStation=COOPGIS_FormGeography.SetSelectionStationByConditionid(Conditionid)
    COOPGIS_FormGeography.GetAndSetDefaultMap(form, ApplicationName, UserNameOrIPAddress)
    SelectionStation, SelectionSpan, GPSStatus=COOPGIS_FormGeography.SetSelectionStationAndSpan(form)
    if(not SelectionStation):
        SelectionStation=COOPGIS_FormGeography.SetSelectionStationByConditionid(Conditionid)
    OpenLayersZoom=COOPGIS_Ajax.GetOpenLayersZoom(SelectionStation, SelectionSpan)      
    if(SelectionStation):
        point = SelectionStation.Point
    else:
        point=None
    NumberOfConditionsListedOnForm=COOPGIS_FormMaintenance.GetNumberOfConditionsListedOnForm(point)
    for conditionFormNumber in range(0, NumberOfConditionsListedOnForm + 1):
        COOPGIS_FormMaintenance.ProcessConditionEditorFormEntry(form, conditionFormNumber, SelectionStation, UserNameOrIPAddress, ApplicationName)
    form=COOPGIS_FormMisc.BuildForm(request, djangoproject.settings.COOPGISConditionEditorApplicationName, DefaultMap, point=point)
    COOPGIS_FormMaintenance.PopulateConditions(form, SelectionStation, NumberOfConditionsListedOnForm)
    COOPGIS_Misc.UpdateSelection(SelectionStation, SelectionSpan, UserNameOrIPAddress)
    #Now Rebuild the final form after processing:

    NumberOfConditionsListedOnForm=COOPGIS_FormMaintenance.GetNumberOfConditionsListedOnForm(point)
    form=COOPGIS_FormMisc.BuildForm(request, djangoproject.settings.COOPGISConditionEditorApplicationName, DefaultMap, point=point)
    COOPGIS_FormMisc.PopulateMiscFormFields(form)
    COOPGIS_FormMaintenance.PopulateConditions(form, SelectionStation, NumberOfConditionsListedOnForm)
    COOPGIS_DynamicTemplate.UpdateConditionCreateTemplate(NumberOfConditionsListedOnForm, UserNameOrIPAddress)
    MapFileName, LayerName=COOPGIS_Misc.GenerateDynamicMapFile(ApplicationName, UserNameOrIPAddress)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/conditioncreate' + UserNameOrIPAddress + '.html')
    c = Context({'OpenLayersZoom': OpenLayersZoom, 'form': form,
                 'LoginStatus':LoginStatus, 'MapFileName': MapFileName, 'LayerName': LayerName,
                 'GPSStatus':GPSStatus, 
                 'ServerIndicator': djangoproject.settings.COOPGISServerIndicator,})
    return HttpResponse(t.render(c))

def ConditionWork(request, Conditionid=None):
    ApplicationName=djangoproject.settings.COOPGISConditionWorkApplicationName
    UserNameOrIPAddress, DefaultMap=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=True, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName) 
    form=COOPGIS_FormMisc.BuildForm(request, djangoproject.settings.COOPGISConditionEditorApplicationName, DefaultMap)
    if(not applicationPermission):  #Show view of Map and Form if no Permissions, do not perform any form processing
        MapFileName, LayerName=COOPGIS_Misc.GenerateDynamicMapFile(ApplicationName, UserNameOrIPAddress)
        OpenLayersZoom=COOPGIS_Ajax.GetOpenLayersZoom(None, None)   
        COOPGIS_DynamicTemplate.UpdateConditionWorkTemplate(1, '')
        t = loader.get_template('/opt/coopgis/djangoproject/templates/conditionwork.html')
        c = Context({'OpenLayersZoom': OpenLayersZoom, 'form': form,
                     'MapFileName': MapFileName, 'LayerName': LayerName, 'LoginStatus':LoginStatus})
        return HttpResponse(t.render(c))
    SelectionStation, SelectionSpan, GPSStatus = COOPGIS_FormGeography.SetSelectionStationAndSpan(form)
    COOPGIS_FormGeography.GetAndSetDefaultMap(form, ApplicationName, UserNameOrIPAddress)
    SelectionStation, SelectionSpan, GPSStatus=COOPGIS_FormGeography.SetSelectionStationAndSpan(form)
    OpenLayersZoom=COOPGIS_Ajax.GetOpenLayersZoom(SelectionStation, SelectionSpan)      
    if(SelectionStation):
        point = SelectionStation.Point
    else:
        point=None
    NumberOfConditionsListedOnForm=COOPGIS_FormMaintenance.GetNumberOfConditionsListedOnForm(point)
    for conditionFormNumber in range(0, NumberOfConditionsListedOnForm + 1):
        COOPGIS_FormMaintenance.ProcessConditionEditorFormEntry(form, conditionFormNumber, SelectionStation, UserNameOrIPAddress, ApplicationName)
    form=COOPGIS_FormMisc.BuildForm(request, djangoproject.settings.COOPGISConditionEditorApplicationName, DefaultMap, point=point)
    COOPGIS_FormMaintenance.PopulateConditions(form, SelectionStation, NumberOfConditionsListedOnForm)
    COOPGIS_Misc.UpdateSelection(SelectionStation, SelectionSpan, UserNameOrIPAddress)
    #Now Rebuild the final form after processing:
    NumberOfConditionsListedOnForm=COOPGIS_FormMaintenance.GetNumberOfConditionsListedOnForm(point)
    form=COOPGIS_FormMisc.BuildForm(request, djangoproject.settings.COOPGISConditionEditorApplicationName, DefaultMap, point=point)
    COOPGIS_FormMisc.PopulateMiscFormFields(form)
    COOPGIS_FormMaintenance.PopulateConditions(form, SelectionStation, NumberOfConditionsListedOnForm)
    COOPGIS_DynamicTemplate.UpdateConditionWorkTemplate(NumberOfConditionsListedOnForm, UserNameOrIPAddress)
    MapFileName, LayerName=COOPGIS_Misc.GenerateDynamicMapFile(ApplicationName, UserNameOrIPAddress)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/conditionwork' + UserNameOrIPAddress + '.html')
    c = Context({'OpenLayersZoom': OpenLayersZoom, 'form': form,
                 'LoginStatus':LoginStatus, 'MapFileName': MapFileName, 'LayerName': LayerName,
                 'GPSStatus':GPSStatus, 
                 'ServerIndicator': djangoproject.settings.COOPGISServerIndicator,})
    return HttpResponse(t.render(c))

def ConditionReview(request, Conditionid=None):
    ApplicationName=djangoproject.settings.COOPGISConditionReviewApplicationName
    UserNameOrIPAddress, DefaultMap=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=True, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName) 
    form=COOPGIS_FormMisc.BuildForm(request, djangoproject.settings.COOPGISConditionEditorApplicationName, DefaultMap)
    if(not applicationPermission):  #Show view of Map and Form if no Permissions, do not perform any form processing
        MapFileName, LayerName=COOPGIS_Misc.GenerateDynamicMapFile(ApplicationName, UserNameOrIPAddress)
        OpenLayersZoom=COOPGIS_Ajax.GetOpenLayersZoom(None, None)   
        COOPGIS_DynamicTemplate.UpdateConditionReviewTemplate(1, '')
        t = loader.get_template('/opt/coopgis/djangoproject/templates/conditionreview.html')
        c = Context({'OpenLayersZoom': OpenLayersZoom, 'form': form,
                     'MapFileName': MapFileName, 'LayerName': LayerName, 'LoginStatus':LoginStatus})
        return HttpResponse(t.render(c))
    SelectionStation, SelectionSpan, GPSStatus = COOPGIS_FormGeography.SetSelectionStationAndSpan(form)
    COOPGIS_FormGeography.GetAndSetDefaultMap(form, ApplicationName, UserNameOrIPAddress)
    SelectionStation, SelectionSpan, GPSStatus=COOPGIS_FormGeography.SetSelectionStationAndSpan(form)
    OpenLayersZoom=COOPGIS_Ajax.GetOpenLayersZoom(SelectionStation, SelectionSpan)      
    if(SelectionStation):
        point = SelectionStation.Point
    else:
        point=None
    NumberOfConditionsListedOnForm=COOPGIS_FormMaintenance.GetNumberOfConditionsListedOnForm(point)
    for conditionFormNumber in range(0, NumberOfConditionsListedOnForm + 1):
        COOPGIS_FormMaintenance.ProcessConditionEditorFormEntry(form, conditionFormNumber, SelectionStation, UserNameOrIPAddress, ApplicationName)
    form=COOPGIS_FormMisc.BuildForm(request, djangoproject.settings.COOPGISConditionEditorApplicationName, DefaultMap, point=point)
    COOPGIS_FormMaintenance.PopulateConditions(form, SelectionStation, NumberOfConditionsListedOnForm)
    COOPGIS_Misc.UpdateSelection(SelectionStation, SelectionSpan, UserNameOrIPAddress)
    #Now Rebuild the final form after processing:
    NumberOfConditionsListedOnForm=COOPGIS_FormMaintenance.GetNumberOfConditionsListedOnForm(point)
    form=COOPGIS_FormMisc.BuildForm(request, djangoproject.settings.COOPGISConditionEditorApplicationName, DefaultMap, point=point)
    COOPGIS_FormMisc.PopulateMiscFormFields(form)
    COOPGIS_FormMaintenance.PopulateConditions(form, SelectionStation, NumberOfConditionsListedOnForm)
    COOPGIS_DynamicTemplate.UpdateConditionReviewTemplate(NumberOfConditionsListedOnForm, UserNameOrIPAddress)
    MapFileName, LayerName=COOPGIS_Misc.GenerateDynamicMapFile(ApplicationName, UserNameOrIPAddress)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/conditionreview' + UserNameOrIPAddress + '.html')
    c = Context({'OpenLayersZoom': OpenLayersZoom, 'form': form,
                 'LoginStatus':LoginStatus, 'MapFileName': MapFileName, 'LayerName': LayerName,
                 'GPSStatus':GPSStatus, 
                 'ServerIndicator': djangoproject.settings.COOPGISServerIndicator,})
    return HttpResponse(t.render(c))



def OutdoorLighting(request, Conditionid=None):
    ApplicationName=djangoproject.settings.COOPGISOutdoorLightingApplicationName
    UserNameOrIPAddress, DefaultMap=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=True, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, djangoproject.settings.COOPGISInventoryCollectionApplicationName)
    form=COOPGIS_FormMisc.BuildForm(request, djangoproject.settings.COOPGISOutdoorLightingApplicationName, DefaultMap)
    if(not applicationPermission):  #Show view of Map and Form if no Permissions, do not perform any form processing
        t = loader.get_template('/opt/coopgis/djangoproject/templates/mainmenu.html')
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))
    SelectionStation, SelectionSpan, GPSStatus = COOPGIS_FormGeography.SetSelectionStationAndSpanOutdoorLighting(form)
    COOPGIS_FormGeography.GetAndSetDefaultMap(form, ApplicationName, UserNameOrIPAddress)
    SelectionStation, SelectionSpan, GPSStatus=COOPGIS_FormGeography.SetSelectionStationAndSpanOutdoorLighting(form)
    OpenLayersZoom=COOPGIS_Ajax.GetOpenLayersZoom(SelectionStation, SelectionSpan)      
    if(SelectionStation):
        point = SelectionStation.Point
    else:
        point=None
    NumberOfConditionsListedOnForm=COOPGIS_FormMaintenance.GetNumberOfOutdoorLightsListedOnForm(point)
    for conditionFormNumber in range(0, NumberOfConditionsListedOnForm + 1):
        COOPGIS_FormMaintenance.ProcessOutdoorLightingFormEntry(form, conditionFormNumber, SelectionStation, UserNameOrIPAddress, ApplicationName)
    form=COOPGIS_FormMisc.BuildForm(request, djangoproject.settings.COOPGISOutdoorLightingApplicationName, DefaultMap, point=point)
    COOPGIS_FormMaintenance.PopulateOutdoorLightingForm(form, SelectionStation, NumberOfConditionsListedOnForm)
    COOPGIS_Misc.UpdateSelection(SelectionStation, SelectionSpan, UserNameOrIPAddress)
    #Now Rebuild the final form after processing:

    NumberOfConditionsListedOnForm=COOPGIS_FormMaintenance.GetNumberOfOutdoorLightsListedOnForm(point)
    form=COOPGIS_FormMisc.BuildForm(request, djangoproject.settings.COOPGISOutdoorLightingApplicationName, DefaultMap, point=point)
    COOPGIS_FormMisc.PopulateMiscFormFields(form)
    COOPGIS_FormMaintenance.PopulateOutdoorLightingForm(form, SelectionStation, NumberOfConditionsListedOnForm)
    COOPGIS_DynamicTemplate.UpdateOutdoorLightingTemplate(NumberOfConditionsListedOnForm, UserNameOrIPAddress)
    MapFileName, LayerName=COOPGIS_Misc.GenerateDynamicMapFile(ApplicationName, UserNameOrIPAddress)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/outdoorlighting' + UserNameOrIPAddress + '.html')
    c = Context({'OpenLayersZoom': OpenLayersZoom, 'form': form,
                 'LoginStatus':LoginStatus, 'MapFileName': MapFileName, 'LayerName': LayerName,
                 'GPSStatus':GPSStatus, 
                 'ServerIndicator': djangoproject.settings.COOPGISServerIndicator,})
    return HttpResponse(t.render(c))




   







